#import "MO2XMLManagedObjectsExporter.h"
#import "MO2XMLManagedObjectExporter.h"

@implementation MO2XMLManagedObjectsExporter

- (id)init {
	self = [super init];

	if(self) {
      [self setManagedObjects:[NSArray array]];
	  [self setProperties:nil];
	}

	return self;	
}

- (void)dealloc {
	[managedObjects release];
	[properties release];
	
	[super dealloc];
}

- (NSArray *)managedObjects {
    return managedObjects;
}

- (void)setManagedObjects:(NSArray *)value {
    [value retain];
	[managedObjects release];
	managedObjects = value;
}

- (NSDictionary *)properties {
	return properties;
}

- (void)setProperties:(NSDictionary *)value {
	[value retain];
	[properties release];
	properties = value;
}

- (NSArray *)managedObjectsAsDictionaries {
	NSMutableArray *dictionaries = [NSMutableArray array];
	NSEnumerator *managedObjectsEnumerator = [[self managedObjects] objectEnumerator];
	id managedObject;
	MO2XMLManagedObjectExporter *exporter = [[[MO2XMLManagedObjectExporter alloc] init] autorelease];
	while(managedObject = [managedObjectsEnumerator nextObject]) {
		[exporter setManagedObject:managedObject];
		[exporter setProperties:[self properties]];
		[dictionaries addObject:[exporter managedObjectAsDictionary]];
	}
			
	return dictionaries;
}

- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)flag {
	NSArray *managedObjectsAsDict = [self managedObjectsAsDictionaries];
	if(managedObjectsAsDict != nil) {
		return [[self  managedObjectsAsDictionaries] writeToFile:path atomically:flag];
	}
	
	return NO;	
}

@end
